<template>
    <a-modal destroyOnClose :title="title" width="660px" :visible="visible"  @cancel="handleClose">
        <a-form-model
            ref="ruleFormModel"
            :model="formModel"
            :rules="rulesModel"
            :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
            :wrapperCol="{ lg: { span: 16 }, sm: { span: 16 } }"
            style="padding-right: 60px"
        >
            <title-name title="基本信息"></title-name>
            <a-row :gutter="24">
                <a-col>
                    <a-form-model-item label="岗位名称" prop="name" class="requiredFormItem">
                        <BaseInput v-model="formModel.name" placeholder="请输入"></BaseInput>
                    </a-form-model-item>
                    <a-form-model-item label="岗位编码" prop="code" class="requiredFormItem">
                        <BaseInput v-model="formModel.code" placeholder="请输入"></BaseInput>
                    </a-form-model-item>
                    <a-form-model-item label="是否是领导" prop="isLeader" class="requiredFormItem">
                        <a-select v-model="formModel.isLeader" placeholder="请选择">
                            <a-select-option v-for="item in isLeaderList" :key="item.id" :value="item.id">{{
                                item.name
                            }}</a-select-option>
                        </a-select>
                    </a-form-model-item>
                </a-col>
            </a-row>
        </a-form-model>
        <template slot="footer" style="text-align: right">
            <a-button :loading="loading" type="primary" @click="handleSubmitPost">提交</a-button>
            <a-button type="primary" class="ml12" @click="handleClose">关闭</a-button>
        </template>
    </a-modal>
</template>

<script>
import ACol from 'ant-design-vue/es/grid/Col'
import ARow from 'ant-design-vue/es/grid/Row'
import TreeSelectOrganization from '@/components/TreeSelectOrganization/index' // 树
import Jobs from '@/components/TreeSelectOrganization/jobs'
import { isLeaderList, type } from './model'
import { addPostManageList, editPostManageList, getEditPostList, addPostByTemplate } from '@/api/system/system'


export default {
    name: 'addEditPost',
    components: {
        ARow,
        ACol,
    },
    props: {
        visible: {
            type: Boolean,
            default: false,
        },
        postInfo: {
            type: Object,
            default() {
                return {}
            },
        },
        postIndex: {
            type: Number,
            default: 1,
        },
    },
    data() {
        return {
            rulesModel: {
                name: [{ required: true, message: '请输入', trigger: 'blur' }],
                code: [{ required: true, message: '请输入', trigger: 'blur' }],
                isLeader: [{ required: true, message: '请选择', trigger: 'blur' }]
            },
            loading: false,
            isLeaderList: isLeaderList,
            type: type,
            orgList: [{}],
            title: '',
            formModel: {
                orgId: '',
                name: '',
                code: '',
                isLeader: undefined,
               
            },
            treeCheckable: true,
            childrenNode: 'jobs',
            form: {
                orgSelectedName: [],
                orgSelectedLabel: [],
                selectedLabel: undefined,
                jobsSelectedName: undefined,
            },
        }
    },
    mounted() {},
    watch: {
        postIndex(val, oldVal) {
            if (val === 1) {
                this.title = '岗位新增'
            } else if (val === 2) {
                this.title = '岗位编辑'
            }
        },
        postInfo(val, oldVal) {
            if (Object.keys(val).length !== 0) {
                // this.form=val
                let isLeader = undefined
                if (val.isLeader === '否') {
                    this.formModel.isLeader = 0
                } else if (val.isLeader === '是') {
                    this.formModel.isLeader = 1
                } else {
                    this.formModel.isLeader = undefined
                }
                this.formModel.name = val.name
                this.formModel.code = val.code
                this.formModel.id = val.id
                 this.formModel.orgId = val.orgId
            }
        },
    },
    methods: {
        /**
         *@desc 提交表单
         *@param {}
         */
        handleSubmitPost() {
            this.$refs.ruleFormModel.validate((valid) => {
                if (valid) {
                    if(this.loading) {
                        return
                    }
                    this.loading=true;
                    let index = this.postIndex
                    if (index === 1) {
                        //用户新增
                        this.handleAddPost()
                    } else if (index === 2) {
                        //用户编辑
                        this.handleEditPost()
                    }
                } else {
                    return false
                }
            })
        },
        /**
         *@desc table 新增
         *@param {}
         */
        handleAddPost() {
            addPostByTemplate(this.formModel)
                .then((res) => {
                    if (res.status === 200) {
                        // 岗位新增成功
                        this.loading=false;
                        this.$message.success('新增成功')
                        this.handleClose()
                    }
                })
                .catch((err) => {
                      this.loading=false;
                    // 岗位新增失败
                    console.log(err)
                })
        },
        /**
         *@desc table 编辑
         *@param {}
         */
        handleEditPost() {
            editPostManageList(this.formModel)
                .then((res) => {
                    if (res.status === 200) {
                        // 岗位编辑成功
                         this.loading=false;
                         this.$message.success('编辑成功')
                        this.handleClose()
                    }
                })
                .catch((err) => {
                    // 岗位编辑失败
                      this.loading=false;
                    console.log(err)
                })
        },
        /**
         *@desc 重置表单
         *@param {}
         */
        handleReset() {
            this.$refs.ruleFormModel.resetFields()
        },
        /**
         *@desc 关闭用户新增
         *@desc 返回用户管理
         *@param {}
         */
        handleClose() {
            // this.handleReset();
            this.$emit('handleCancel')
        },
    },
}
</script>

<style scoped>
</style>